/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server.lastproject;

import Server.MyDataType.MyStructTable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author CANG
 */
public class MyPopMenu_TableStruct extends JPopupMenu
{
    private JMenuItem[] _jmiItems;
    private ActionListener _acl;
    private ServerForm _server;
    private String _nameTableOwner;
    private JTable _jtableStruct = null;
    
    public MyPopMenu_TableStruct(ServerForm server, JTable jTableStruct,String NameTable)
            
    {
        _jtableStruct = jTableStruct;
        _nameTableOwner = NameTable;
        _server = server;
        _jmiItems = new JMenuItem[3];        
        _jmiItems[0] = new JMenuItem("Update Table");
        _jmiItems[1] = new JMenuItem("Delete Column");
        _jmiItems[2] = new JMenuItem("Insert Column");
        
        
        for(int i = 0; i < _jmiItems.length; i++)
        {
            add(_jmiItems[i]);        
        }        
        
        _acl = new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                String command = e.getActionCommand();
                char c = command.charAt(0);
                
                switch(c)
                {
                    case 'S':
                    {
                        String sNewNameTable = (String) JOptionPane.showInputDialog(_jtableStruct, "Name Table","Server",
                            JOptionPane.INFORMATION_MESSAGE,null,null,"NewTable");

                    //người dùng click cancel
                    if (sNewNameTable == null) {
                         
                        return;
                    }
                    if (sNewNameTable.equals("")) {
                        JOptionPane.showMessageDialog(_server, "Name table not null!", "Server", JOptionPane.ERROR_MESSAGE, null);
                        return;
                    }
                    MyStructTable structTable = _server.getMyStructTable(_jtableStruct.getModel());
                    boolean bCreateNewTable = false;
                    bCreateNewTable = _server.createTable(structTable, sNewNameTable);
                    _server.LoadDataTable();
                    if (!bCreateNewTable) {
                        JOptionPane.showMessageDialog(_server, "Save Structure table "+_nameTableOwner + " fail!", "Server", JOptionPane.ERROR_MESSAGE, null);
                    } 
                    else
                        
                    {
                         JOptionPane.showMessageDialog(_jtableStruct, "save table "+_nameTableOwner +" success!");
                    }
                        break;
                    }
                        
                    case 'U':
                    {
                        MyStructTable myTable = _server.getMyStructTable(_jtableStruct.getModel());                        
                        boolean bUpdate = false;
                        int iSize = myTable._vtAttribute.size();
                        if(iSize != 0)
                        {    
                            bUpdate = _server.updateStructTable(myTable, _nameTableOwner);                        
                        }
                        
                        if(!bUpdate)
                        {
                            JOptionPane.showMessageDialog(_jtableStruct, "Update table "+_nameTableOwner + " fail !", "Server", JOptionPane.ERROR_MESSAGE, null );
                        }
                        else
                        {
                             JOptionPane.showMessageDialog(_jtableStruct, "Update table "+_nameTableOwner + " success!");
                        }
                        break;
                    }
                        
                    case 'I':
                    {
                        TableModel dataModel = _jtableStruct.getModel();
                        int iAmountColumn = dataModel.getColumnCount();
                        String[] sNewColumn = new String[iAmountColumn];
                        ((DefaultTableModel)dataModel).addRow(sNewColumn);                        
                        break;
                    }
                        
                    case 'D':
                    {
                        try
                        {
                           
                            int iRowSelect = _jtableStruct.getSelectedRow();
                            String nameColumn = null;
                            if(iRowSelect > 0)
                            {
                                nameColumn = (String)_jtableStruct.getValueAt(iRowSelect, 0);                            
                                boolean bDelete = _server.deleteColumnStruct(_nameTableOwner, nameColumn);
                                DefaultTableModel tableModel =  (DefaultTableModel)_jtableStruct.getModel();
                                tableModel.removeRow(iRowSelect);

                            }
                        }
                        catch(Exception ex)
                        {
                            int b = 4;
                            b--;
                        }
                        break;
                    }
                }
            }
        };
        
        for(int i = 0; i < _jmiItems.length; i++)
        {
            _jmiItems[i].addActionListener(_acl);
        }
        
        
    }
    
    public void setDataTableOwner(String nameTable)
    {
        _nameTableOwner = nameTable;
    }
    
    public void setCreateTable()
    {
        _jmiItems[0].setText("Save Table");
    }
    
    public void setUpdateTable()
    {
        _jmiItems[0].setText("Update Table");        
    }
    
}
